// pages/lyric/index.js
import { debounce,throttle } from "../../utils/util"
import { getpropose,searchSong} from "../../utils/request/Api"
const app = getApp()
const user = wx.getStorageSync('user')
Page({

  /**
   * 页面的初始数据
   */
  data: {
   theme:"",
   value:"",
   landscape: ['https://img1.baidu.com/it/u=682342695,202492647&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281',
   'https://img2.baidu.com/it/u=2434201168,269190915&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500',
   'https://img1.baidu.com/it/u=1927287361,1667152263&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500',
   'https://img0.baidu.com/it/u=788044902,85816046&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800'],
   autoplay:true,
   gridlist:[
     {
       id:1,text:"每日推荐",icon:'../../assets/icon/today.png',path:"../daily/index"
     },
     {
      id:2,text:"排行榜",icon:'../../assets/icon/ranking.png',path:""
    },
    {
      id:3,text:"电台",icon:'../../assets/icon/fm.png',path:""
    }, {
      id:4,text:"相似歌曲",icon:'../../assets/icon/monitor-two.png',path:""
    }
   ],
   albumList:[],
   propose:[],
   newPropose:[],
   songPlay:[],
   newSongPlay:[],
   show:false,
   searchList:[]
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      theme: app.globalData.theme,
    });
    this.getPropose()
   
   
  },
  //获取推荐歌单
  getPropose(){
    const data = {
      user_markings :user.user_markings
    }
  const res =  getpropose(data)
  res.then(res=>{
    if(res.code == 200){
      const {albumList,propose,songPlay} = res.data
      this.setData({
        albumList,
        propose:this.formatDataForDisplay(propose),
        newPropose:propose,
        songPlay:this.formatDataForDisplay(songPlay),
        newSongPlay:songPlay
      })
    }
  })
  },
  //
  //跳转歌单详情
  onClickAlbum(e){
    const {album_markings} = e.currentTarget.dataset
    wx.navigateTo({
      url: '../albumsong/index?item='+JSON.stringify(album_markings),
    })
  },
  //跳转到歌词
  onClickPropose(e){
    const {index,float} =e.currentTarget.dataset
   
    if(float=='true'){
      const {newPropose} = this.data
      this.songLyric(newPropose,index)
      return
    }else{
      const {newSongPlay} = this.data
      this.songLyric(newSongPlay,index)
      return
    }
   
  },
  //封装跳转歌词
  songLyric(text,index){
    wx.setStorageSync('songlist', JSON.stringify(text))
    wx.setStorageSync('musicPlaybackState', {song:JSON.stringify(text[index])})
    wx.setStorageSync('index', index)
     wx.navigateTo({
       url: '../lyric/index?songlist='+JSON.stringify(text)+"&index="+index,
     })
     wx.setStorageSync('flag', {isRunning: true})
  },
  //图标跳转
  onClickTap(e){
    const {path} = e.currentTarget.dataset
    wx.navigateTo({
      url: path,
    })
  },
  //处理数据
  formatDataForDisplay: function(arr) {
    let propose = [];
    let globalIndex =0
    for (let i = 0; i < Math.ceil(arr.length / 3); i++) {
      const column = arr.slice(i * 3, (i + 1) * 3).map(song=>{
        return{...song,globalIndex:globalIndex++};
      })
      propose.push(column);
    }
    
   return propose;
  },
  onChange:throttle(function(e) {
    if(e.detail!=""){
      this.setData({
        show:true
      })
      const obj = {
        text:e.detail
      }
      searchSong(obj).then(res=>{
        this.setData({
          searchList:res.data.list
        })
        console.log(res);
      })
    }else{
     this.setData({
       show:false
     })
    }
  },0),
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})